package org.cooperativa.controller;

import java.io.Serializable;
import java.util.Date;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJBException;
import javax.enterprise.context.SessionScoped;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;
import javax.inject.Named;
import org.cooperativa.controller.util.JsfUtil;
import org.cooperativa.dao.TransaccionFacade;
import org.cooperativa.entities.Socio;
import org.cooperativa.entities.Transaccion;

@Named(value = "transaccionController")
@SessionScoped
public class TransaccionController extends AbstractController<Transaccion> implements Serializable {

    @Inject
    private TransaccionFacade ejbFacade;

    private Transaccion current;
    
    private Socio socio;
    //variables usadas para mostrar u ocultar los botonoes de crear
    private Boolean transCreada = false;
   
    public TransaccionController() {
        super(Transaccion.class);
    }

    @PostConstruct
    public void init() {
        super.setFacade(ejbFacade);
    }
        
    //------------Getters y Setters-----------
    public Transaccion getCurrent() {
        if (current == null) {
            current = new Transaccion();            
        }
        return current;
    }
    
    public Socio getSocio() {
        return socio;
    }

    public void setSocio(Socio socio) {
        this.socio = socio;
    }
    
    public Boolean getTransCreada() {
        return transCreada;
    }

    public void setTransCreada(Boolean transCreada) {
        this.transCreada = transCreada;
    }
    
        
    public void verificarPagoAnterior(){
        
        Boolean resultado;
        
        resultado = ejbFacade.verficarPagoAnterior(socio);  
    
    }
    
    public String navegarCreate(){
        current = null;
        
        return "/transaccion/Paso1.xhtml?faces-redirect=true";
    }
    
    /*****Metodo utilizado para crear un nuevo transaccion******/
    public void createNew(ActionEvent event) {
        String successMessage = ResourceBundle.getBundle("/Bundle").getString("TransaccionCreated");        
        
        try {
                //antes de almacenar la nueva instacion debemos asignar los valores para auditoria de la BD
                current.setCreatedBy(1); //modificar luego para que ponga el id del usuario
                current.setCreationDate(new Date());
            
                ejbFacade.create(current);
                transCreada = true;                               
                
                JsfUtil.addSuccessMessage(successMessage);
            } catch (EJBException ex) {
                String msg = "";
                Throwable cause = JsfUtil.getRootCause(ex.getCause());
                if (cause != null) {
                    msg = cause.getLocalizedMessage();
                }
                if (msg.length() > 0) {
                    JsfUtil.addErrorMessage(msg);
                } else {
                    JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
                }
            } catch (Exception ex) {
                Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
                JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            }
    }
        
}
